home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / LIFE6__ / PROTO / P / PW_LIFE_.C < prev    next >
Text File  |  1991-08-16  |  15KB  |  508 lines

  1. /*  PW_Life_4a
  2.  
  3. File name: Life_4a
  4. Function: Handle a Window
  5. /* History: 8/13/91 Original by Prototyper 3.0   */
  6.  
  7. #include "PCommonLife6.h"    /* Common */
  8. #include "Common_Life6.h"    /* Common */
  9. #include "PUtils_Life6.h"    /* General Utilities */
  10. #include "Utils_Life6.h"    /* General Utilities */
  11.  
  12.  
  13. #include "PW_Life_4a.h"    /* This file */
  14.  
  15. #include "Life_4a.h"    /* The window specific file */
  16. static struct {
  17.         long            Id;
  18.         STATUS_TYPE        Health;
  19.         ControlHandle     CHandle;    
  20.         Rect            CRect;
  21.         }map[MaxRow][MaxCol], new_map[MaxRow][MaxCol];
  22.  
  23.  
  24. /* ======================================================= */
  25.  
  26.  
  27. static ControlHandle ScrollHHandle;                                      /* Scrollbar for horz scrolling */
  28. static ControlHandle ScrollVHandle;                                      /* Scrollbar for vert scrolling */
  29.  
  30. static void Do_A_Button(ControlHandle theControl);                  /* Handle a button being pressed */
  31.  
  32.  
  33.  
  34. /* ======================================================= */
  35.  
  36. /* Routine: Init_Life_4a */
  37. /* Purpose: Initialize our window data to not in use yet */
  38.  
  39. void Init_Life_4a()
  40. {
  41.  
  42.  
  43.  
  44.         WPtr_Life_4a = NIL;                                                 /* Make sure other routines know we are not valid yet */
  45.  
  46.         ScrollHHandle = NIL;                                                  /* Scrollbar is not valid yet */
  47.         ScrollVHandle = NIL;                                                  /* Scrollbar is not valid yet */
  48.  
  49.  
  50.         U_Init_Life_4a();                                                     /* Call the user window init routine */
  51.  
  52. }
  53.  
  54.  
  55. /* ======================================================= */
  56.  
  57. /* Routine: Close_Life_4a */
  58. /* Purpose: Close out the window */
  59.  
  60. void Close_Life_4a(whichWindow)
  61. WindowPtr    whichWindow;
  62. {
  63.  
  64.  
  65.  
  66.         if ((WPtr_Life_4a != NIL) && ((WPtr_Life_4a == whichWindow) || (whichWindow == (WindowPtr)-1))) /* See if we should close this window */
  67.             {
  68.  
  69.                 U_Close_Life_4a();                                             /* Call the user window close routine */
  70.  
  71.  
  72.                 DisposeWindow(WPtr_Life_4a);                              /* Clear window and controls */
  73.                 WPtr_Life_4a = NIL;                                           /* Make sure other routines know we are closed */
  74.             }                                                                      /* End for if (MyWindow<>nil) */
  75.  
  76. }                                                                                /* End of procedure */
  77.  
  78.  
  79. /* ======================================================= */
  80.  
  81. /* Routine: Resized_Life_4a */
  82. /* Purpose: We were resized or zoomed, update the scrolling scrollbars */
  83.  
  84. void Resized_Life_4a(OldRect, whichWindow)                          /* Resized this window */
  85. Rect        *OldRect;
  86. WindowPtr    whichWindow;
  87. {
  88.         WindowPtr    SavePort;                                              /* Place to save the last port */
  89.         Rect    temp2Rect;                                                    /* temp rectangle */
  90.         short    Index;                                                         /* temp integer */
  91.  
  92.         if (WPtr_Life_4a == whichWindow)                                /* Only do if the window is us */
  93.             {
  94.                 GetPort(&SavePort);                                          /* Save the current port */
  95.                 SetPort(WPtr_Life_4a);                                       /* Set the port to my window */
  96.  
  97.                 U_Resized_Life_4a(OldRect);                                 /* Call the user window resized routine */
  98.  
  99.                 temp2Rect = WPtr_Life_4a->portRect;                     /* Get the window rectangle */
  100.                 EraseRect(&temp2Rect);                                      /* Erase the new window area */
  101.                 InvalRect(&temp2Rect);                                       /* Set to update the new window area */
  102.  
  103.                 SetPort(SavePort);                                             /* Restore the old port */
  104.             }                                                                      /* End for window is us */
  105. }                                                                                /* End of procedure */
  106.  
  107.  
  108.  
  109. /* ======================================================= */
  110.  
  111. /* Routine: Moved_Life_4a */
  112. /* Purpose: We were moved, possibly to another screen and screen depth */
  113.  
  114. void Moved_Life_4a(OldRect, whichWindow)                            /* Moved this window */
  115. Rect        *OldRect;
  116. WindowPtr    whichWindow;
  117. {
  118.         WindowPtr    SavePort;                                              /* Place to save the last port */
  119.  
  120.         if (WPtr_Life_4a == whichWindow)                                /* Only do if the window is us */
  121.             {
  122.                 GetPort(&SavePort);                                          /* Save the current port */
  123.                 SetPort(WPtr_Life_4a);                                       /* Set the port to my window */
  124.  
  125.                 U_Moved_Life_4a(OldRect);                                  /* Call the user window moved routine */
  126.  
  127.                 SetPort(SavePort);                                             /* Restore the old port */
  128.             }                                                                      /* End for window is us */
  129. }                                                                                /* End of procedure */
  130.  
  131.  
  132. /* ======================================================= */
  133.  
  134. /* Routine: Update_Life_4a */
  135. /* Purpose: Update our window */
  136.  
  137. void Update_Life_4a(whichWindow)
  138. WindowPtr    whichWindow;
  139. {
  140.         WindowPtr    SavePort;                                              /* Place to save the last port */
  141.  
  142.  
  143.  
  144.         if ((WPtr_Life_4a != NIL) && (WPtr_Life_4a == whichWindow))/* Handle the update to our window */
  145.             {
  146.                 GetPort(&SavePort);                                          /* Save the current port */
  147.                 SetPort(WPtr_Life_4a );                                      /* Set the port to my window */
  148.  
  149.                 TextFont(systemFont);                                         /* Select the Font that we want */
  150.                 /* Draw a string of text, Static Text  */
  151.                 SetRect(&tempRect, 16,0,236,15);
  152.                 GetIndString(sTemp,Res_Ctrl_Static_Text,1);            /* Get the string */
  153.                 TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
  154.  
  155.                 /* Draw a string of text, Static Text  */
  156.                 SetRect(&tempRect, 16,16,232,31);
  157.                 GetIndString(sTemp,Res_Ctrl_Static_Text2,1);          /* Get the string */
  158.                 TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
  159.  
  160.                 /* Draw a string of text, Static Text  */
  161.                 SetRect(&tempRect, 84,32,164,47);
  162.                 GetIndString(sTemp,Res_Ctrl_Static_Text3,1);          /* Get the string */
  163.                 TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
  164.  
  165.                 /*  Draw a line, Drawn line1  */
  166.                 PenSize(3,3);                                                   /* Change pen size to draw a thicker line */
  167.                 MoveTo(0,160);                                                /* Horz,vert, Move to starting position */
  168.                 LineTo ( 255,160);                                             /* Horz,vert, Draw to ending position */
  169.                 PenSize(1,1);                                                   /* Restore the pen size to default */
  170.  
  171.                 TextFont(applFont);                                             /* Back to the application font */
  172.  
  173.  
  174.                 U_Update_Life_4a();                                           /* Call the user window update routine */
  175.  
  176.                 DrawControls(WPtr_Life_4a );                               /* Draw all the controls */
  177.                 SetPort(SavePort);                                             /* Restore the old port */
  178.             }                                                                      /* End for if (MyWindow<>nil) */
  179. }                                                                                /* End of procedure */
  180.  
  181. /* ======================================================= */
  182.  
  183. /* Routine: Open_Life_4a */
  184. /* Purpose: Open our window */
  185.  
  186. void Open_Life_4a()
  187. {
  188.         
  189.         register counterX = 1;
  190.         register counterY = 1;
  191.         register index = LoBase;
  192.         long    theLong;                                                         /* Used for icons and hotspots definition */
  193.  
  194.         if (WPtr_Life_4a == NIL)                                             /* See if already opened */
  195.             {
  196.  
  197.                 WPtr_Life_4a= GetNewWindow(Res_W_Life_4a,NIL, (Ptr)-1);/* Get the window from the resource file */
  198.                 tempRect = WPtr_Life_4a->portRect;                       /* Get window size, we will now center it */
  199.                 tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;/* Center vert */
  200.                 if (tempRect.top < 40)                                         /* Keep below the menu bar */
  201.                     tempRect.top = 40;
  202.                 tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;/* Center Horz */
  203.                 MoveWindow(WPtr_Life_4a, tempRect.left, tempRect.top, TRUE);/* Now move the window to the proper position */
  204.                 SetPort(WPtr_Life_4a);                                       /* Prepare to write into our window */
  205.  
  206.                 /*  Make a button, GO  */
  207.                 Ctrl_GO = GetNewControl( Res_Ctrl_GO, WPtr_Life_4a);/* Make a new button */
  208.  
  209.  
  210.  
  211. /* INITIALIZE THE ZONES & ARRAY HERE */
  212.             for (; index <= HiBase; index++)
  213.             {
  214.                 /* xxx   */
  215.                 map[counterY][counterX].CHandle = GetNewControl( index, WPtr_Life_4a);/* Make a new HotSpot */
  216.                 HideControl(map[counterY][counterX].CHandle);                           /* Hide till we set all the parameters */
  217.                 theLong = 0x00FF0000;                                       /* Tell the CDEF that it is a HotSpot */
  218.                 theLong = theLong + (long)((0x00FF & 2) * 0x00000100);/* Set the line width parameter */
  219.                 (*map[counterY][counterX].CHandle)->contrlData = (Handle)theLong;/* Set all the parameters */
  220.                 map[counterY][counterX].CRect = (*map[counterY][counterX].CHandle)->contrlRect;/* Get the position */
  221.                 ShowControl(map[counterY][counterX].CHandle);                          /* Show since we set all the parameters  */
  222.                 
  223.                 map[counterY][counterX].Id = index;
  224.                 
  225.                 if (counterX < (MaxCol - 1))
  226.                 {
  227.                     ++counterX;
  228.                 }
  229.                 else
  230.                 {
  231.                     ++counterY;
  232.                     counterX = 1;
  233.                 }
  234.             }
  235.             
  236.  
  237.                 U_Open_Life_4a();                                             /* Call the users window open routine */
  238.  
  239.                 ShowWindow(WPtr_Life_4a);                                 /* Show the window now */
  240.  
  241.             }                                                                      /* End for if (MyWindow<>nil) */
  242.         else
  243.             SelectWindow(WPtr_Life_4a);                                   /* Already open, so show it */
  244.  
  245. }                                                                                /* End of procedure */
  246.  
  247.  
  248. /* ======================================================= */
  249.  
  250. /* Routine: Activate_Life_4a */
  251. /* Purpose: We activated or deactivated. */
  252.  
  253. void Activate_Life_4a(whichWindow,Do_An_Activate)               /* Activated or deactivated this window */
  254. WindowPtr    whichWindow;
  255. Boolean Do_An_Activate;
  256. {
  257.         WindowPtr    SavePort;                                              /* Place to save the last port */
  258.  
  259.  
  260.  
  261.         if (WPtr_Life_4a == whichWindow)                                /* Only do if the window is us */
  262.             {
  263.                 GetPort(&SavePort);                                          /* Save the current port */
  264.                 SetPort(WPtr_Life_4a);                                       /* Set the port to my window */
  265.  
  266.                 if (Do_An_Activate == TRUE)                                 /* Handle the activate */
  267.                     {
  268.  
  269.                     }                                                                /* End for activate */
  270.                 else
  271.                     {                                                                /* Start of deactivate */
  272.  
  273.                         if (theInput != NIL)                                        /* See if there is already a TE area */
  274.                             TEDeactivate(theInput);                              /* Yes, so turn it off */
  275.                         theInput = NIL;                                            /* Deactivate the TE area */
  276.                     }                                                                /* End for deactivate */
  277.  
  278.                 U_Activate_Life_4a(Do_An_Activate);                     /* Call the user window activate routine */
  279.  
  280.                 SetPort(SavePort);                                             /* Restore the old port */
  281.             }                                                                      /* End for window is us */
  282. }                                                                                /* End of procedure */
  283.  
  284.  
  285. /* ======================================================= */
  286.  
  287. /* Handle a button pressed */
  288.  
  289. static void Do_A_Button(theControl)                                     /* Handle a button being pressed */
  290. ControlHandle    theControl;
  291. {
  292.         long    RefCon;                                                         /* RefCon for controls */
  293.         register counterX = 1;
  294.         register counterY = 1;
  295.  
  296.  
  297.  
  298.         HiliteControl(theControl, 10);                                      /* Darken the button */
  299.         RefCon = GetCRefCon(theControl);                                 /* get control refcon */
  300.  
  301.         if (RefCon == Res_Ctrl_GO)
  302.         {
  303.         /* THROW PROCEDURE CALL TO COMPUTE GENERATION HERE */
  304.         COMPUTE();
  305.         }
  306.         
  307. /***************************************************************/
  308. /*    THIS ROUTINE CHECKS FOR THE STATUS OF A CELL WHEN CLICKED */
  309.         while (map[counterY][counterX].Id != RefCon)
  310.         {
  311.             if (counterX < (MaxCol - 1))
  312.                 {
  313.                     ++counterX;
  314.                 }
  315.                 else
  316.                 {
  317.                     ++counterY;
  318.                     counterX = 1;
  319.                 }
  320.         }
  321.         
  322.             if ((map[counterY][counterX].Id == RefCon) && (map[counterY][counterX].Health == DEAD))
  323.                 {
  324.                     map[counterY][counterX].Health = ALIVE;
  325.                     InvertRect (&map[counterY][counterX].CRect);
  326.                 }
  327.                 else if ((map[counterY][counterX].Id == RefCon) && (map[counterY][counterX].Health == ALIVE))
  328.                 {
  329.                     map[counterY][counterX].Health = DEAD;
  330.                     InvertRect (&map[counterY][counterX].CRect);
  331.                 }
  332. /***************************************************************/
  333.                 
  334.                                                                         /* end of switch */
  335.  
  336.         
  337.         U_DoButton_Life_4a(RefCon,theControl);                         /* Call to handle any extra user buttons */
  338.  
  339.         HiliteControl(theControl, 0);                                        /* Lighten the button */
  340. }                                                                                /* End of Handle a button being pressed */
  341.  
  342.  
  343. /* ======================================================= */
  344.  
  345. /* Routine: Do_Life_4a */
  346. /* Purpose: Handle action to our window, like controls */
  347.  
  348. void Do_Life_4a(myEvent)
  349. EventRecord        *myEvent;
  350. {
  351.         short    code;                                                          /* Location of event in window or controls */
  352.         WindowPtr    whichWindow;                                         /* Window pointer where event happened */
  353.         Point    myPt;                                                         /* Point where event happened */
  354.         ControlHandle    theControl;                                         /* Handle for a control */
  355.  
  356.         if (WPtr_Life_4a != NIL)                                             /* Handle only when the window is valid */
  357.             {
  358.                 code = FindWindow(myEvent->where, &whichWindow);/* Get where in window and which window */
  359.  
  360.                 if (WPtr_Life_4a == whichWindow)
  361.                     U_DoEvent_Life_4a(myEvent);
  362.  
  363.                 if ((myEvent->what == mouseDown) && (WPtr_Life_4a == whichWindow))
  364.                     {
  365.                         myPt = myEvent->where;                              /* Get mouse position */
  366.                         GlobalToLocal(&myPt);                                 /* Make it relative */
  367.  
  368.                     }
  369.  
  370.                 if ((WPtr_Life_4a == whichWindow) && (code == inContent))/* for our window */
  371.                     {
  372.  
  373.                         code = FindControl(myPt, whichWindow, &theControl);/* Get type of control */
  374.  
  375.                         if (code != 0)                                              /* Check type of control */
  376.                             code = TrackControl(theControl,myPt, (Ptr)-1);/* Track the control */
  377.                         if (code == inButton)
  378.                             Do_A_Button(theControl);                           /* Do buttons */
  379.  
  380.                     }                                                                /* End for if (MyWindow=whichWindow) */
  381.             }                                                                      /* End for if (MyWindow<>nil) */
  382. }                                                                                /* End of procedure */
  383.  
  384.  
  385. /* ======================================================= */
  386. /* ======================================================= */
  387.  
  388. COMPUTE(void)
  389.  
  390. {    
  391.     int counterY,counterX;
  392.     for(counterY=1;counterY <= (MaxRow - 1);counterY++)
  393.     {
  394.         for(counterX=1;counterX <= (MaxCol - 1);counterX++)
  395.         {
  396.             switch(NEIGHBORCOUNT(counterY,counterX))
  397.             {/* zzz */
  398.                 case 0:
  399.                 case 1:
  400.                     new_map[counterY][counterX].Health = DEAD;
  401.                     break;
  402.                 
  403.                 case 2:
  404.                     new_map[counterY][counterX].Health = map[counterY][counterX].Health;
  405.                     break;
  406.                 
  407.                 case 3:
  408.                     new_map[counterY][counterX].Health = ALIVE;
  409.                     break;
  410.                     
  411.                 case 4:
  412.                 case 5:
  413.                 case 6:
  414.                 case 7:
  415.                 case 8:
  416.                     new_map[counterY][counterX].Health = DEAD;
  417.                     break;
  418.             }/* switch */
  419.         }/* for counterX */
  420.     }/* for counterY*/
  421.     CopyMap(); 
  422. }
  423.  
  424.  
  425.  
  426. /* ======================================================= */
  427. int NEIGHBORCOUNT(int countY, int countX)
  428. {
  429.     int i,j;
  430.     int rlow,rhigh;
  431.     int clow, chigh;
  432.     int count = 0;
  433.     
  434.     if (countY <= 0)
  435.         rlow = 0;
  436.     else 
  437.         rlow = countY - 1;
  438.         
  439.     if (countY >= MaxRow - 1)
  440.         rhigh = MaxRow - 1;
  441.     else
  442.         rhigh= countY + 1;
  443.         
  444.     if (countX <= 0)
  445.         clow = 0;
  446.     else 
  447.         clow = countX - 1;
  448.         
  449.     if (countX >= MaxCol - 1)
  450.         chigh = MaxCol - 1;
  451.     else
  452.         chigh= countX + 1;
  453.  
  454.     for (i = rlow; i <= rhigh; i++)
  455.         for (j = clow; j <= chigh; j++)
  456.             if (map[i][j].Health == ALIVE)
  457.                 count++;
  458.     
  459.     if (map[countY][countX].Health == ALIVE)
  460.                 count--;
  461.                 
  462.     return count;
  463. }
  464. /* ======================================================= */
  465. CopyMap()
  466.  
  467. {
  468.     int counterY,counterX;
  469.     for(counterY=1;counterY <= (MaxRow - 1);counterY++)
  470.     {
  471.         for(counterX=1;counterX <= (MaxCol - 1);counterX++)
  472.         {
  473.             if (map[counterY][counterX].Health != new_map[counterY][counterX].Health)
  474.             {
  475.                 InvertRect (&map[counterY][counterX].CRect); 
  476.             }
  477.             map[counterY][counterX].Health = new_map[counterY][counterX].Health;
  478.         }    
  479.     }
  480.     DrawControls(WPtr_Life_4a );
  481. }
  482.  
  483.  
  484.  
  485.  
  486. /* ======================================================= */
  487. RESET()
  488.  
  489. {
  490.     int counterY,counterX;
  491.     
  492.     for(counterY=1;counterY <= (MaxRow - 1);counterY++)
  493.         for(counterX=1;counterX <= (MaxCol - 1);counterX++)
  494.         {
  495.             if (map[counterY][counterX].Health == ALIVE)
  496.             {
  497.                 map[counterY][counterX].Health = DEAD;
  498.                 InvertRect (&map[counterY][counterX].CRect); 
  499.             }
  500.         }    
  501.     DrawControls(WPtr_Life_4a );
  502.     
  503. }
  504.  
  505.  
  506.  
  507. /* ======================================================= */
  508.